package com.pactera.asmp.server.session;

import org.crazycake.shiro.exception.SerializationException;
import org.crazycake.shiro.serializer.RedisSerializer;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import io.jsonwebtoken.lang.Assert;

/**
 * @ProjectName: asmp-server
 * @Package com.pactera.asmp.server.session
 * @ClassName: SessionCacheRedisSerializer
 * @Description:
 * @Date 2020/08/02 10:44
 * @Author lvzhiqiang
 * @Version 1.0
 */
public class SessionCacheRedisSerializer implements RedisSerializer {
    private final Charset charset;

    public SessionCacheRedisSerializer() {
        this(StandardCharsets.UTF_8);
    }

    public SessionCacheRedisSerializer(Charset charset) {
        Assert.notNull(charset);
        this.charset = charset;
    }

    @Override
    public byte[] serialize(Object o) throws SerializationException {
        return (o == null ? null : o.toString().getBytes(charset));
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        return (bytes == null ? null : new String(bytes, charset));
    }
}
